<?php
require_once('cache_database.inc');

function js_include_once($scripts, $nocache=false)
{
	if (!is_array($scripts)) {
		js_include_once_single($scripts, $nocache);
	}
	else {
		foreach($scripts as $script) {
			js_include_once_single($script, $nocache);
		}
	}
}

function js_include_once_single($script_name, $nocache=false)
{
	global $GENERAL_CONFIG;
	static $scripts = array();
	static $noncached_scripts = array();
	static $aggregator_rebuild = false;
	global $all_js;

	$cache_name = str_replace("/", ".", $_SERVER['SCRIPT_NAME']);
	$cache_name .= "." . ".all.js";
	$cache_name = ltrim($cache_name,".");
	$cache_name = str_replace(".php", "", $cache_name);
	
	$_SESSION[$cache_name] = &$scripts;
	
	$db = cache_database::Instance();
	
	if (!isset($scripts[$script_name])) {
		if ((!$GENERAL_CONFIG['aggregate'] || $nocache) && (!isset($noncached_scripts[$script_name]))) {
			$noncached_scripts[$script_name] = true;
			$all_js .= "\n<script src='" . $script_name . "?version=" . $GENERAL_CONFIG['version'] . "'></script>";		
		}
		else if ($GENERAL_CONFIG['aggregate'] && !$nocache) {
			$cache_info = $db->GetCacheInfo($cache_name);
			if (!$cache_info) {
				$db->CreateCacheInfo($cache_name);
				$cache_info = $db->GetCacheInfo($cache_name);
			}
			
			if (count($scripts) == 0) { // called for the first time for the page being loaded
				$all_js_version = $cache_info['version'];
				$all_js = '<script src="/cache/aggcache/' . $all_js_version . '.' . $cache_name . '"></script>';
			}

			if (!check_file_in_cache_and_up_to_date($script_name, $cache_info)
			    && (!$aggregator_rebuild)) {
				register_shutdown_function('aggregate', $cache_name, "js");
				$aggregator_rebuild = true;
				global $old_js_cache_must_refresh;
				$old_js_cache_must_refresh = true;
				$all_js = "";
			}
			
			$scripts[$script_name] = true;
		}
	}
}



function css_include_once($css_name, $nocache=false)
{
	global $GENERAL_CONFIG;
	static $csss = array();
	static $noncached_csss = array();
	static $aggregator_rebuild = false;	
	global $all_css;

	$cache_name = str_replace("/", ".", $_SERVER['SCRIPT_NAME']);
	$cache_name .= "." . ".all.css";
	$cache_name = ltrim($cache_name,".");
	$cache_name = str_replace(".php", "", $cache_name);
	
	$_SESSION[$cache_name] = &$csss;
	$db = cache_database::Instance();
	
	if (!isset($csss[$css_name])) {
		if ((!$GENERAL_CONFIG['aggregate'] || $nocache)  && (!isset($noncached_csss[$css_name]))) {
			$noncached_csss[$css_name] = true;
			echo '<link rel="stylesheet" type="text/css" href="' . $css_name .  "?version=" . $GENERAL_CONFIG['version'] . '" />';
		}
		else if ($GENERAL_CONFIG['aggregate'] && !$nocache) {
			$cache_info = $db->GetCacheInfo($cache_name);
			if (!$cache_info) {
				$db->CreateCacheInfo($cache_name);
				$cache_info = $db->GetCacheInfo($cache_name);
			}
						
			if (count($csss) == 0) {
				$all_css_id = $cache_info['version'];
				$all_css = '<link rel="stylesheet" type="text/css" href="/cache/aggcache/' 
					 . $all_css_id . '.' . $cache_name . '" />';
			}
			
			if (!check_file_in_cache_and_up_to_date($css_name, $cache_info)
			    && (!$aggregator_rebuild)) {
				register_shutdown_function('aggregate', $cache_name, "css");
				$aggregator_rebuild = true;
				global $old_css_cache_must_refresh;
				$old_css_cache_must_refresh = true;
				$all_css = "";
			}
			
			$csss[$css_name] = true;
		}
	}
}

function aggregate($cache_name, $cache_type) {
	$files = array_keys($_SESSION[$cache_name]);
	$db = cache_database::Instance(true);
	$cache_info = $db->GetCacheInfo($cache_name);
	$files_in_cache = array();
	if ($cache_info['cache_content'] != "") {
		$files_in_cache = explode(",", $cache_info['cache_content']);
	}
	$all_files = array_union($files, $files_in_cache);
	$aggregated = "";
	foreach($all_files as $file_name) {
		$aggregated .= "\n/*" . $file_name . "*/\n";
		$aggregated .= file_get_contents($_SERVER['DOCUMENT_ROOT'] . $file_name);
	}
	$version = md5(uniqid(mt_rand(), true));
	file_put_contents($_SERVER['DOCUMENT_ROOT'] . '/cache/aggcache/' . $version . '.' . $cache_name, $aggregated);
	
	$db->SetCacheInfo($cache_name, implode(',', $all_files), $version, time());	
	
	switch($cache_type) {
		case "js":		
			echo '<script src="/cache/aggcache/' . $version . '.' . $cache_name . '"></script>';
			break;
		case "css":
			echo '<link rel="stylesheet" type="text/css" href="/cache/aggcache/' . $version . '.' . $cache_name . '" />';
			break;
		default: break;
	}
}

function array_union($a, $b) {
  $union = array_merge($a, $b); // duplicates may still exist
  $union = array_unique($union);
 
  return $union;
}

function check_file_in_cache_and_up_to_date($filename, $cache_info)
{
	$scripts_in_cache = explode(",", $cache_info['cache_content']);

	$cache_filename = '/cache/' . $cache_info['version'] . '.' . $cache_info['cache_name'];
	
	if (!is_file($cache_filename)) {
		return false;
	}
	
	if (!in_array($filename, $scripts_in_cache)) {
		return false;
	}
	
	$last_update_time = $cache_info['last_update_unix'];

	clearstatcache();
	$modify_time = filemtime($_SERVER['DOCUMENT_ROOT'] . $filename);
		
	if ($modify_time > $last_update_time)
		return false;
	
	return true;
}
?>